#define _CRT_SECURE_NO_WARNINGS 1

#include <algorithm>
#include <vector>

using namespace std;
class Solution {
public:
    int triangleNumber(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        int ret = 0;
        for (int c = nums.size() - 1; c > 1; --c)
        {
            int a = 0;
            int b = c - 1;
            while (a < b)
            {
                if (nums[a] + nums[b] > nums[c])
                {
                    ret += b - a;
                    --b;
                }
                else
                {
                    ++a;
                }
            }
        }
        return ret;
    }
};